package com.qf.day14.homework;

import java.util.*;

public class PokerGame {
    public static void main(String[] args) {
        // 1. 生成一副扑克牌（不包括大小王）
        List<String> pokerCards = generatePokerCards();
        // 2. 洗牌
        Collections.shuffle(pokerCards);
        // 3. 给三个玩家发牌，底牌剩三
        Map<String, List<String>> players = dealCards(pokerCards, 3);
        // 显示每个玩家当前的牌
        for (Map.Entry<String, List<String>> entry : players.entrySet()) {
            System.out.println("玩家 " + entry.getKey() + " 的手牌:");
            for (String card : entry.getValue()) {
                System.out.print(card + " ");
            }
            System.out.println();
        }
        // 显示底牌
        List<String> bottomCards = pokerCards.subList(pokerCards.size() - 3, pokerCards.size());
        System.out.println("底牌: " + bottomCards);
        // 4. 计算每个玩家手中的炸弹数量
        for (Map.Entry<String, List<String>> entry : players.entrySet()) {
            int bombs = countBombs(entry.getValue());
            System.out.println("玩家 " + entry.getKey() + " 手中的炸弹数量: " + bombs);
        }
    }

    private static List<String> generatePokerCards() {
        List<String> cards = new ArrayList<>();
        String[] suits = {"♠", "♥", "♦", "♣"};
        String[] points = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        for (String suit : suits) {
            for (String point : points) {
                cards.add(suit + point);
            }
        }
        return cards;
    }

    private static Map<String, List<String>> dealCards(List<String> pokerCards, int playersCount) {
        Map<String, List<String>> players = new LinkedHashMap<>();
        int cardsPerPlayer = pokerCards.size() / playersCount;
        for (int i = 0; i < playersCount; i++) {
            players.put("玩家" + (i + 1), new ArrayList<>());
        }
        for (int i = 0; i < pokerCards.size() - 3; i++) { // 留下最后三张作为底牌
            players.get("玩家" + ((i / cardsPerPlayer) + 1)).add(pokerCards.get(i));
        }
        return players;
    }

    private static int countBombs(List<String> cards) {
        Map<String, Integer> pointCount = new HashMap<>();
        for (String card : cards) {
            String point = card.substring(1); // 移除花色标识，只保留点数
            pointCount.put(point, pointCount.getOrDefault(point, 0) + 1);
        }
        int bombs = 0;
        for (int count : pointCount.values()) {
            if (count == 4) { // 四张相同的牌算作一个炸弹
                bombs++;
            }
        }
        return bombs;
    }
}
